<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worklog extends MY_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('worklog_m');
		$this->user_id = $this->session->userdata('user_id');
		$this->user_name = $this->session->userdata('user_name');
	}

	/*工作日志*/
	function _index_post($log_date,$check = false){

			if($check){
				$this->rejsonsuccess($this->worklog_m->worklog_check($log_date)>0);
			}else{
				$arr = $this->input->post();
				if(empty($arr['log_am']) && empty($arr['log_pm'])){
					$data = array();
					if($this->worklog_m->worklog_check($log_date)>0){
						$data = $this->worklog_m->worklog_check($log_date,true);
					}
					$this->load->view('worklog/new',$data);
				}else{
					$arr['log_date'] = $log_date;
					$this->rejsonsuccess($this->worklog_m->worklog_new($arr),'该日期已有数据，不能重复添加');
				}
				
			}

	}
	function worklog_is_b($log_date){
		$arr = array();
		$data = $this->worklog_m->worklog_is_b($log_date);
		foreach ($data as $row){
			$arr1 = explode("-",$row['log_date']);
			if(substr($arr1[1],0,1)==0){ $arr1[1] =  substr($arr1[1],1);}
			if(substr($arr1[2],0,1)==0){ $arr1[2] =  substr($arr1[2],1);}
			$arr[] = $arr1[0].'-'.$arr1[1].'-'.$arr1[2];
		}
		echo json_encode(array("success"=>true,'m'=>$log_date,'o'=>$arr));
	}
	
	function worklog_list(){
		$this->load->view('worklog/worklog_list');
	}
	
	function worklog_manage(){
		$this->load->view('worklog/worklog_manage');
	}
	
	function _log_data_post(){
		if($this->acl->check_privilege('worklog_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->worklog_m->getlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['id'],"log_am"=>$v['log_am'],"log_pm"=>$v['log_pm'],"log_morn"=>$v['log_morn'],"log_date"=>$v['log_date'],'log_w'=>$v['log_w']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}

	function _log_data_manage_post(){
		if($this->acl->check_privilege('worklog_manage') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->worklog_m->getmanagelistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['id'],"dept_name"=>$v['dept_name'],"role_name"=>$v['role_name'],"user_id"=>$v['user_id'],"user_name"=>$v['user_name'],"log_am"=>$v['log_am'],"log_pm"=>$v['log_pm'],"log_morn"=>$v['log_morn'],"log_date"=>$v['log_date'],'log_w'=>$v['log_w']);
				}
			}
			if(empty($data['total']))$data['total'] = 0;
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}

	function get_tree_stint($data, $pid = 0, $key = 'id', $pKey = 'parentId', $childKey = 'children', $maxDepth = 0){  
		static $depth = 0;  
		$depth++;  
		if (intval($maxDepth) <= 0){  
			$maxDepth = count($data) * count($data);  
		}
		if ($depth > $maxDepth){  
			//exit("递归错误：最大递归深度 {$maxDepth}");  
		}  
		$tree = array();  
		foreach ($data as $rk => $rv){  
			if ($rv[$pKey] == $pid){  
				$rv[$childKey] = $this->get_tree_stint($data, $rv[$key], $key, $pKey, $childKey, $maxDepth);  
				$tree[] = $rv;  
			}  
		}  
		return $tree;  
	} 

	function getdeptdata_stint(){//权限树列表JSON格式数据
		
		$data = $this->worklog_m->deptlist_stint('');
		$depttree = $this->get_tree_stint($data['data'],$data['p_id']);
		echo json_encode($depttree); 
	}
	
	public function _log_del_post(){
		$data=$this->input->post();
		$data = $this->worklog_m->del_worklog($data);
		$this->rejsonsuccess($data,"'删除工作日志'失败，请联系管理员");		
	}
	
	function advanced_search($m){
		$data['v'] = array('m'=>$m);
		$this->load->view('worklog/worklog_search',$data);
	}
	
	function worklog_table(){
		$result = $this->worklog_m->worklog_table($this->input->post());
		/*
		$searchParams = '';
		if($this->input->post('user_id'))
			$searchParams['user_id'] = $this->input->post('user_id');//开始时间由于没有日期，默认从0点开始
		if($this->input->post('role_id'))
			$searchParams['role_id'] = $this->input->post('role_id');//开始时间由于没有日期，默认从0点开始
		if($this->input->post('wids'))
			$searchParams['wids'] = $this->input->post('wids');//开始时间由于没有日期，默认从0点开始
		if($this->input->post('wide'))
			$searchParams['wide'] = $this->input->post('wide'); 
			//$searchParams['wide'] = date('Y-m-d',strtotime($this->input->post('wide')." + 1 day"));
			
		$result=$this->worklog_m->worklog_table($searchParams);//总条数
		*/
		if(!$result)
			return false;
//print_r($result);
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("Sheet1")->setDescription("none");
		//setActiveSheetIndex(0):操作第一个工作表
		
		//列字段
		//$abc =array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN');

		//全局设置
		$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);  //页面方向横向，纵向为：ORIENTATION_PORTRAIT
		$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);  //纸张大小
		
		//设置打印页边距
		$objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.5);
		$objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.5);
		$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
		$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.5);
		
		$count_row = count($result)+3;
		$objPHPExcel->getActiveSheet()->getStyle('A3:G'.$count_row)->getAlignment()->setWrapText(true);//自动换行
		$objPHPExcel->getActiveSheet()->getStyle('A3:G'.$count_row)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);   //设置单元格为实线
		$objPHPExcel->getActiveSheet()->getStyle('A3:G'.$count_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中
		$objPHPExcel->getActiveSheet()->getStyle('E4:G'.$count_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); //水平居中
		$objPHPExcel->getActiveSheet()->getStyle('A3:G'.$count_row)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
		$objPHPExcel->getActiveSheet()->getStyle('A3:G'.$count_row)->getFont()->setSize(9); //字体大小

		//标题区
		$objPHPExcel->getActiveSheet()->setCellValue('A1','工作日志');//插入
		$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');  //合并单元格
		$objStyleA1 = $objPHPExcel->getActiveSheet()->getStyle('A1'); //对A1进行设置
		$objAlignA1 = $objStyleA1->getAlignment();  //对齐方式
		$objAlignA1->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中
		$objAlignA1->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
		
		$objFontA1 = $objStyleA1->getFont();  //字体
		$objFontA1->setName('黑体');  
		$objFontA1->setSize(20);  //10号字体
		
		//日期元素
		$objPHPExcel->getActiveSheet()->setCellValue('A2',date('Y-m-d'));//插入
		$objPHPExcel->getActiveSheet()->mergeCells('A2:G2');  //合并单元格
		$objStyleA1 = $objPHPExcel->getActiveSheet()->getStyle('A2'); //对A2进行设置
		$objAlignA1 = $objStyleA1->getAlignment();  //对齐方式
		$objAlignA1->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); //水平居左
		$objAlignA1->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中


		//设置列字段标题
		$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue('A3', '部门')
			->setCellValue('B3', '职务')
			->setCellValue('C3', '姓名')
			->setCellValue('D3', '日期')
			->setCellValue('E3', '上午')
			->setCellValue('F3', '下午')
			->setCellValue('G3', '明天预安排');
		
		
		//列宽
		$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(16); //部门
		$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setWidth(12); //职务
		$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setWidth(9); //姓名
		$objPHPExcel->getActiveSheet()->getColumnDimension("D")->setWidth(12); //日期
		$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setWidth(28); //上午
		$objPHPExcel->getActiveSheet()->getColumnDimension("F")->setWidth(28); //下午
		$objPHPExcel->getActiveSheet()->getColumnDimension("G")->setWidth(28); //明天预安排
		
		//行高
		$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(35);//行高
		$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);//行高
		$objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(20);//行高
		

		
		// Field names in the first row
		/*
		$fields = $query->list_fields();
		$col = 0;
		foreach ($fields as $field){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
			$col++;
		}*/
		// Fetching the table data
		$fields =array('dept_name','role_name','user_name','log_date','log_am','log_pm','log_morn');
		
		$row = 4;
		foreach($result as $data){
			$col = 0;
			foreach ($fields as $field){
				$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
				$col++;
			}
				

			$row++;
		}

		$objPHPExcel->setActiveSheetIndex(0);

		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

		//发送标题强制用户下载文件
		//header('Content-Type: application/vnd.ms-excel');
		//header('Content-Disposition: attachment;filename="abc_'.date('Y-m-d').'.xls"');
		//header('Cache-Control: max-age=0');

		//$objWriter->save('php://output');
		$objWriter->save(iconv('UTF-8','GBK','worklog.xls'));
		
		if(file_exists("worklog.xls")){
			$status = true;
		}else{
			$status = false;
		}
		echo json_encode(array("success"=>$status)); 
	}
	
	function worklog_down(){
		$this->load->helper('download');
		$data = file_get_contents("worklog.xls"); // 读文件内容
		$name = 'worklog.xls';
		force_download($name, $data);
	}
}
?>